<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增部门' : '编辑部门'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="状态" prop="delFlag">
        <el-radio v-for="item in dicts" :key="item.value" v-model="form.delFlag" :label="item.value">{{ item.name }}</el-radio>
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;" label="上级部门">
        <treeselect v-model="form.parentId" :options="depts" style="width: 370px;" placeholder="选择上级类目" />
        <!--<treeselect v-model="form.parentId" style="width: 370px;" placeholder="选择上级类目" >-->
        <!--<el-option-->
        <!--v-for="item in depts"-->
        <!--:key="item.id"-->
        <!--:label="item.name"-->
        <!--:is-default-expanded="true"-->
        <!--:is-disabled="item.delFlag==1?true:false"-->
        <!--:children="item.children"-->
        <!--/>-->
        <!--</treeselect>-->
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/* eslint-disable */
import { save, update, getDepts } from '@/api/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import Config from '@/config'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    dicts: {
      type: Array,
      required: true
    },
  },
  data() {
    return {
      loading: false,
      dialog: false,
      depts: [],
      form: {
        delFlag: '0'
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.parentId !== undefined) {
            this.loading = true
            if (this.isAdd) {
              this.doAdd()
            } else this.doEdit()
          } else {
            this.$message({
              message: '上级部门不能为空',
              type: 'warning'
            })
          }
        }
      })
    },
    doAdd() {
      save(this.form).then(res => {
        if (res.code == Config.code_200) {
          this.resetForm()
          this.$notify({ title: '添加成功', type: 'success' })
          this.$parent.initAllData()
        } else {
          this.$notify.error({ title: '失败', message: res.msg })
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      update(this.form).then(res => {
        if (res.code == Config.code_200) {
          this.resetForm()
          this.$notify({ title: '修改成功', type: 'success' })
          this.$parent.initAllData()
        } else {
          this.$notify.error({ title: '失败', message: res.msg })
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    },
    getDepts() {
      getDepts().then(res => {
      // this.depts=res.data
        //循环遍历，将name字段替换成label   笨方法
        // <!--<treeselect v-model="form.parentId" :options="depts" style="width: 370px;" placeholder="选择上级类目" />-->
        var jsonObj = JSON.parse(JSON.stringify(res.data));
        this.depts=[];
        for (var i = 0; i < jsonObj.length; i++) {
          var dept=[];
          dept['id']=jsonObj[i].id;
          dept['label']=jsonObj[i].name;
          dept['isDefaultExpanded']=true;
          dept['isDisabled']=jsonObj[i].delFlag === 1?true:false;
          if (jsonObj[i].children !==undefined && jsonObj[i].children.length > 0){
            var jsonO = JSON.parse(JSON.stringify(jsonObj[i].children));
            dept['children']=this.parseChildrenDepts(jsonO)
          }
          this.depts.push(dept)
        }
      })
    },
    parseChildrenDepts(jsonObj){
      var dept=[];
      for (var i = 0; i < jsonObj.length; i++) {
        var childrenDept=[];
        childrenDept['id']=jsonObj[i].id;
        childrenDept['label']=jsonObj[i].name;
        childrenDept['isDefaultExpanded']=true;
        childrenDept['isDisabled']=jsonObj[i].delFlag===1?true:false;
        if (jsonObj[i].children !==undefined && jsonObj[i].children.length > 0){
          var jsonObj = JSON.parse(JSON.stringify(jsonObj[i].children));
          childrenDept['children']=this.parseChildrenDepts(jsonObj)
        }
        dept.push(childrenDept)
      }
      return dept
    },
  }
}
</script>

<style scoped>

</style>
